from selenium.webdriver import ActionChains
from hyrobot.common import STEP, CHECK_POINT, INFO
from create_table_super_num import *
import pyautogui
import pytest
import time
import json
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
from pymysql_connect import *
from lib.webui_local_mysql import *

class c73252:
    name = '查看所有存在的产品，核对产品管理的数量，核对10条产品管理-c73252'  # 测试用例名字

    # 初始化方法
    # def setup(self):
    #     STEP(1, '登陆网站')
    #     # 超级管理员登录
    #     driver = open_browser()
    #     mgr_login(driver)
    #
    # # 清除方法
    # def teardown(self):
    #     driver = get_global_webdriver()
    #     driver.quit()

    # 测试用例步骤
    def teststeps(self):
        # 获取webdriver对象 对应 已经登录好的浏览器
        # driver = get_global_webdriver()

        STEP(2, "核对总数与产品管理员实际创建的可见产品数量是否一致")
        db = connect()
        # 使用cursor()方法获取操作游标

        cursor = db.cursor()
        sql = "select count(case when is_display=1 then 1 end) as calculate_active_num ,\
                       count(case when is_display=0 then 1 end) as calculate_disable_num from test_kylin_web.product_num ;"

        try:
            # 执行SQL语句
            cursor.execute(sql)
            # 获取所有记录列表
            results = cursor.fetchall()
            for row in results:
                active_num = row[0]
                inactive_num = row[1]
        except:
            INFO("1Error: unable to fetch data")

        cursor_product_manage_sup = db.cursor()
        product_manage_sup_sql = "select count(case when is_display=1 then 1 end) as calculate_active_num ,\
                                    count(case when is_display=0 then 1 end) as calculate_disable_num from test_kylin_web.product_num_for_superior;"

        try:
            # 执行SQL语句
            cursor_product_manage_sup.execute(product_manage_sup_sql)
            # 获取所有记录列表
            product_manage_sup_result = cursor_product_manage_sup.fetchall()
            for row in product_manage_sup_result:
                product_manage_sup_active_num = row[0]
                product_manage_sup_inactive_num = row[1]
        except:
            INFO("2Error: unable to fetch data")
        CHECK_POINT("核对产品列表总数与产品管理员实际创建的可见产品数量是否一致",
                    active_num == product_manage_sup_active_num and inactive_num == product_manage_sup_inactive_num)

        STEP(3, "从产品管理员账号的产品列表中挑选10条与超级管理员产品列表核对详细的产品信息")
        cursor_product_list_detail = db.cursor()
        product_list_detail_sql = "SELECT product_name,version,creater,create_time,update_time FROM test_kylin_web.product_num WHERE creater = '张三' OR creater = '张31';"

        try:
            # 执行SQL语句
            cursor_product_list_detail.execute(product_list_detail_sql)
            # 获取所有记录列表
            product_list_detail_result = cursor_product_list_detail.fetchall()
            require_list = list(list(items) for items in list(product_list_detail_result))

        except:
            INFO("3Error: unable to fetch data")

        # from productor
        cursor_product_list_detail_productor = db.cursor()
        product_list_detail_productor_sql = "SELECT product_name,version,creater,create_time,update_time FROM test_kylin_web.product_num_for_superior WHERE creater = '张三' OR creater = '张31';"

        try:
            # 执行SQL语句
            cursor_product_list_detail_productor.execute(product_list_detail_productor_sql)
            # 获取所有记录列表
            product_list_detail_productor_result = cursor_product_list_detail_productor.fetchall()
            require_list_productor = list(list(items) for items in list(product_list_detail_productor_result))
            CHECK_POINT("比较得到数据列表是否一致", sorted(require_list_productor) == sorted(require_list))
        except:
            INFO("4Error: unable to fetch data")

        db.close()
